#!/usr/bin/env bash

red='\e[91m'
green='\e[92m'
yellow='\e[93m'
magenta='\e[95m'
cyan='\e[96m'
none='\e[0m'
blue='\033[34m'

# $@ 表示所有的参数列表。
# $# 表示传递给脚本的参数数量。
# $! 表示后台运行的最后一个进程的进程号。
# $0 表示脚本名
# 在 awk 中，$1 表示第一个字段，$2 表示第二个字段， $NF 表示最后一个字段

# sudo chmod 777 $0

# # Root
# [[ $(id -u) != 0 ]] && echo -e " 请使用 ${red}root ${none}用户运行 ${yellow}~(^_^) ${none}" && exit 1

get_passwd() {
  echo
  read -s -rp "请 输 入 开 机 密 码：" password
  echo
}

change_root(){
  if [[ $(id -u) != 0 ]];then
    if [ -z "$password" ]; then
      get_passwd
      export PASSWORD="$password"; sudo -v -S <<< "$PASSWORD"
    else
      sudo -v -S <<< "$PASSWORD"
    fi
  else
    sudo chmod 777 "$0"
  fi
}

# 获取系统架构
sys_str=$(uname -m)
# 修复系统软件标志位
software_repair_flag=''
# 文件标志位
file_flag=0
# 下载标志位
download_flag=''
# 获取普通用户
this_user=$(users | awk '{print $1}')
# 获取当前时间
timestamp=$(date +%Y-%m-%d_%H_%M_%S)
# 获取芯片，$1=$1用于去除字段前后的空格或制表符。
cpu_name=$(lscpu | grep "型号名称" | awk -F "：" '{print $2}' | awk '{$1=$1;print}')
# 获取根目录
sys_root=$(lsblk -o NAME,MOUNTPOINT -P | grep -w / | awk -F" " '{print $1}' | awk -F"=" '{print $2}' | awk -F"\"" '{print $2}')
# 获取当前文件夹名NF取最后一个--如/home/leeyc/桌面，则桌面
# dir=$(pwd|awk -F "/" '{print $NF}')
  os_version_small=$(cat /etc/kylin-build | grep -w "buildid" | awk -F" " '{print $2}')       # 获取buildid，比如2203-x86=31950，2303-x86=41998


# #确认当前系统是否为V10桌面版本
version_all=$(grep "Desktop-V10" /etc/.kyinfo)

if [[ $version_all == *"Desktop-V10-SP1"* ]]; then
  version_flag="V10SP1"
else
  version_flag="V10"
fi

#获取普通用户目录
home_path() {
  cd "/home/$this_user" || exit
}

desktop_path() {
  if [ -d "/home/$this_user/桌面" ]; then
    cd "/home/$this_user/桌面" || exit
  elif [ -d "/home/$this_user/desktop" ]; then
    cd "/home/$this_user/desktop" || exit
  fi
}

#暂停
get_pause() {
  SAVEDSTTY=$(stty -g)
  stty -echo
  stty cbreak
  dd if=/dev/tty bs=1 count=1 2>/dev/null
  stty -raw
  stty echo
  stty "$SAVEDSTTY"
}

#回到主菜单
return_menu(){
  echo
  echo
  echo -e "按任意键返回$yellow主菜单$none"
  get_pause
  echo
  menu
}

# print信息
show_print() {
  local options=("$@")
  for ((i=0; i<${#options[@]}; i++)); do
    echo
    echo -e "$blue ${options[$i]}$none"
  done
}

# menu显示菜单
show_menu() {
  echo
  echo -e "$green 0. $red 按 0 退出$none"
  local options=("$@")
  for ((i=0; i<${#options[@]}; i++)); do
    echo
    echo -e "$green $((i+1)). $red ${options[$i]}$none"
  done
  echo
  echo -e "温馨提示...如果你不想执行选项...按$magenta 0 $none或者 $magenta Ctrl + C $none即可退出"
  echo
}
# menu选择菜单
menu_selection() {
  read -r -p "$(echo -e "请选择菜单 [${green}0-$((${#menu_options[@]} - 1))$none]:")" choose
  if [[ -z $choose || ! $choose =~ ^[0-9]+$ || $choose -lt 0 || $choose -ge ${#menu_options[@]} ]]; then
    exit 1
  else
    selected_function="${menu_options[choose]}"
    eval "$selected_function"
  fi
}

#1.系统信息
system_message() {
  head_print=(
    ".......................系统相关信息.........................."
  )
  options=(
    "查询系统信息"
    "查看实时网速"
    "备份日志到桌面"
    "查看系统运行信息"
  )
  show_print "${head_print[@]}"
  show_menu "${options[@]}"
  menu_options=(
    ["0"]="return_menu"
    ["1"]="system_info"
    ["2"]="net_speed"
    ["3"]="backup_log"
    ["4"]="sys_operation_info"
  )
  menu_selection
}

#1.1 获取系统基本信息
system_info() {
  change_root
  head_print=(
    ".......................请等待10秒左右.........................."
  )
  show_print "${head_print[@]}"
  version=$(sudo cat /etc/os-release | grep -w "VERSION" | awk -F"=" '{print $2}')              #系统版本
  version_small=$(sudo cat /etc/kylin-build | grep -w "Build" | awk -F" " '{print $2}')         #小版本号
  kylin_serial=$(sudo kylin_serial)                                                             #服务序列号
  if [ -f "/etc/.kyactivation" ]; then
    kyactivation_key=$(sudo cat /etc/.kyactivation 2>/dev/null)                                 #激活码
  else
    kyactivation_key=None
  fi                                                                     
  kyregistration_key=$(sudo kylin_gen_register)                                                 #注册码
  system_serial_number=$(sudo cat /sys/class/dmi/id/product_serial)                             #主机序列号
  blk_serials=$(sudo lsblk -d -o NAME,SERIAL,TYPE -P | grep disk)                               #硬盘序列号
  phy_adapters=$(sudo ls /sys/class/net/ | grep -v "$(ls /sys/devices/virtual/net/)")           #网卡

  # os_version=$(head -n1 /etc/kylin-build |awk '{print $2}')                                   # 获取系统版本
  #cpu_name=Kirin990,2203version=20230310,os_version_small=40222
  echo -e "电脑cpu为: $cpu_name"
  echo -e "操作系统版本为: $version"
  echo -e "操作系统小版本号为: $version_small"
  echo -e "服务序列号为：$kylin_serial"
  echo -e "激活码为: $kyactivation_key"
  echo -e "注册码: $kyregistration_key"
  echo -e "主机序列号: $system_serial_number"
  echo -e "硬盘序列号: $blk_serials"
  echo -e "网卡为: $phy_adapters"
  echo -e "网卡MAC地址:"
  for phy_adapter in $phy_adapters; do
    mac=$(cat /sys/class/net/"$phy_adapter"/address)
    echo "NAME=\"$phy_adapter\" MAC=\"$mac\""
  done
  echo -e "根目录为：$sys_root"
  return_menu
}

#1.2 实时网速
net_speed() {
  echo -e " In ------ Out"
  read -r -p "$(echo -e "请输入测试时长（秒）：")" duration
  start_time=$(date +%s)
  while [[ $(date +%s) -lt $((start_time + duration)) ]]; do
    OLD_IN=$(awk '{print $2}' /proc/net/dev | awk 'NR==4{print $1}')
    OLD_OUT=$(awk '{print $10}' /proc/net/dev | awk 'NR==4{print $1}')
    sleep 1
    NEW_IN=$(awk '{print $2}' /proc/net/dev | awk 'NR==4{print $1}')
    NEW_OUT=$(awk '{print $10}' /proc/net/dev | awk 'NR==4{print $1}')
    IN=$(printf "%.1f%s" "$(( ("$NEW_IN" - "$OLD_IN") / 1024 ))" "KB/s")
    OUT=$(printf "%.1f%s" "$(( ("$NEW_OUT" - "$OLD_OUT") / 1024 ))" "KB/s")
    echo "$IN $OUT"
  done
  get_pause
}
#1.3 备份log日志
backup_log(){
  change_root
  sudo tar -czvPf /home/"$this_user"/桌面/log_"$timestamp".tar.gz /var/log
  sudo chmod 777 /home/"$this_user"/桌面/log_"$timestamp".tar.gz
  echo -e "$magenta 找到log_$timestamp.tar.gz这个文件，备份到桌面了，从桌面拷出来发给我$none \n"
  get_pause
}
#1.4 查看系统运行信息
sys_operation_info(){
  date | tee -a /home/"$this_user"/桌面/stat_cpu_mem.txt
  echo "占用cpu最高的10个进程是:" | tee -a /home/"$this_user"/桌面/stat_cpu_mem.txt
  ps aux --sort=-%cpu|sed -n '1,11p' | tee -a /home/"$this_user"/桌面/stat_cpu_mem.txt
  echo "占用mem最高的10个进程是:" | tee -a /home/"$this_user"/桌面/stat_cpu_mem.txt
  ps aux --sort=-%mem|sed -n '1,11p' | tee -a /home/"$this_user"/桌面/stat_cpu_mem.txt
  echo "总体内存使用情况是:"  | tee -a /home/"$this_user"/桌面/stat_cpu_mem.txt
  free -h | tee -a /home/"$this_user"/桌面/stat_cpu_mem.txt
  echo "中断情况是:"  | tee -a  /home/"$this_user"/桌面/stat_cpu_mem.txt
  tee -a /home/"$this_user"/桌面/stat_cpu_mem.txt <<< "$(</proc/interrupts)"
  echo  | tee -a  /home/"$this_user"/桌面/stat_cpu_mem.txt
  echo -e "$red 桌面有个stat_cpu_mem.txt文件，可打开查看信息"
  chmod 777 /home/"$this_user"/桌面/stat_cpu_mem.txt
  get_pause
}

#2.部分系统bug修复
system_repair() {
  head_print=(
    ".......................系统修复.........................."
  )
  options=(
    "系统激活相关问题--如激活时不出现二维码等问题"
    "桌面任务栏、图标、黑屏、控制面板打不开等相关问题"
    "ll查看命令不生效--误删.bashrc和.bash_profile文件"
    "在开发盘下尝试fsck修复根目录"
    "在开发盘下挂载固态盘"
    "2203系统，刻录软件续刻时报错"
    "系统升级-备份重要数据--没啥事儿不要乱升级"
    "增加一些系统功能--如：exfat硬盘支持、触摸板、开机启动、校准时间"
  )
  show_print "${head_print[@]}"
  show_menu "${options[@]}"
  menu_options=(
    ["0"]="return_menu"
    ["1"]="activation"
    ["2"]="desk_top"
    ["3"]="no_bashrc"
    ["4"]="fsck -y /dev/nvme0n1p2"
    ["5"]="mount_disk"
    ["6"]="download_file kelu_xuke deb_zip"
    ["7"]="update_sys"
    ["8"]="sys_func"
  )
  menu_selection
}

#2.1 系统激活相关
activation() {
  change_root
  head_print=(
    ".......................系统激活相关报错及相关功能.........................."
  )
  options=(
    "修复激活时提示无效序列号--下载对应系统版本的kyinfo文件"
    "修复激活时提示无法获取硬件信息错误码0017问题"
    "命令交互式激活操作系统"
    "查看系统相关信息(版本、序列号、注册码、激活码)"
  )
  show_print "${head_print[@]}"
  show_menu "${options[@]}"
  menu_options=(
    ["0"]="return_menu"
    ["1"]="activation_download"
    ["2"]="sudo rm -rf /etc/.kyhwid && sudo kylin_activation_check && get_pause"
    ["3"]="sudo kylin-system-verify && get_pause"
    ["4"]="system_info"
  )
  menu_selection
}

activation_download() {
  head_print=(
    ".......................先看看电脑时间对不对，不对的话继续.........................."
  )
  options=(
    "安装V10-0710版本"
    "安装V10-1026版本"
    "安装V10-0710-2版本"
    "安装V10-1026-2版本"
    "安装V10-2107版本"
    "安装V10-2203版本"
    "安装V10SP1-2107版本"
    "安装V10SP1-2203版本"
    "安装V10SP1-2303版本"
  )
  show_print "${head_print[@]}"
  show_menu "${options[@]}"
  menu_options=(
    ["0"]="return_menu"
    ["1"]="download_file act_v10_0710 act_zip"
    ["2"]="download_file act_v10_1026 act_zip"
    ["3"]="download_file act_v10_0710_2 act_zip"
    ["4"]="download_file act_v10_1026_2 act_zip"
    ["5"]="download_file act_v10_2107 act_zip"
    ["6"]="download_file act_v10_2203 act_zip"
    ["7"]="download_file act_v10sp1_2107 act_zip"
    ["8"]="download_file act_v10sp1_2203 act_zip"
    ["9"]="download_file act_v10sp1_2303 act_zip"
  )
  menu_selection
}

#2.2 桌面任务栏、图标、黑屏、控制面板打不开等相关问题
desk_top(){
  change_root
  head_print=(
    ".......................桌面任务栏、图标、黑屏、控制面板打不开等相关问题.........................."
  )
  options=(
    "尝试修复系统任务栏消失---修改分辨率后仍旧不出现"
    "桌面右下角不显示小键盘图标，且调不出输入法"
    "开机后桌面图标无显示，或显示正在启动caja，只有任务栏"
    "开机进tty1--黑屏(命令行),不进桌面"
    "桌面密钥环清除"
    "输入密码进不去桌面，跳回登录页面（原因之一：磁盘数据满，尝试清除部分数据）---此功能同清理垃圾--清理垃圾时注意在系统无问题时清理"
    "V10非SP1提示面板载入问题，任务栏显示有问题,执行完重启如果还报错执行8"
    "V10控制面板打不开,如果还报错执行9"
    "重装卸载残留的文件--注意重要文件要做备份，适用于安装或卸载软件导致系统出问题"
    "关机卡麒麟软件四个字---特定情况出现：如安装身份鉴别等软件出现问题--lightdm报错缺pam_gnome_keyring.so\pam_kwallet.so\pam_kwallet.so错误"
  )
  show_print "${head_print[@]}"
  show_menu "${options[@]}"
  read -r -p "$(echo -e "$magenta 请选择数字 : $none ")" _opt
  echo
  if [[ -z $_opt ]]; then
    error
  else
    case $_opt in
    0)
      menu
      ;;
    1)
      #系统任务栏消失
      home_path
      sudo rm -rf .cache/ .config/ .Xauthority
      ;;
    2)
      #右下角无小键盘及输入法
      home_path
      sudo rm -rf .config/
      sudo systemctl restart lightdm
      ;;
    3)
      #开机桌面图标无显示
      sudo apt update
      sudo rm -rf .config/
      sudo apt install caja --reinstall
      ;;
    4)
      #开机进tty1-桌面caja有问题--重装caja和重装重置lightdm
      sudo apt update
      sudo apt install caja --reinstall
      sudo apt install lightdm --reinstall
      sudo dpkg-reconfigure lightdm
      sudo systemctl enable lightdm
      ;;
    5)
      #桌面密钥环消除
      sudo apt install seahorse -y
      echo
      echo
      echo -e "$red 三秒后在打开的界面右键点密码下的key，删除即可$none"
      echo
      echo
      sleep 3
      sudo seahorse
      ;;
    6)
      #清理垃圾、输入密码进不去桌面，跳回登录页面
      sudo apt clean
      sudo apt autoclean                                                #删/var/cache/apt/archives下deb文件
      sudo apt autoremove                                               #删不需要的依赖
      sudo dpkg -l | grep '^rc' | awk '{print $2}' | xargs sudo dpkg --purge #删软件残留配置--rc开头的文件
      sudo rm -rf /tmp/*                                                #删tmp缓存
      ;;
    7)
      #面板载入问题
      if [[ $version_flag == 'V10' ]];then
        sudo apt install mate-indicators --reinstall
      fi
      ;;
    8)
      #控制面板打不开
      if [[ $version_flag == 'V10' ]];then
        sudo apt install kylin-control-center --reinstall
        sudo kylin-control-center
      fi
      ;;
    9)
      #重装卸载残留的文件--适用于安装或卸载软件导致系统出问题
      sudo dpkg -P upstart
      sudo dpkg -P firefox
      sudo apt install aptitude
      sudo dpkg -l | grep '^rc' | awk '{print $2}' | xargs sudo aptitude install -y
      ;;
    10)
      #无法关机
      sudo sed -i  "/keyring/s/^\(.*\)$/#&/g" /etc/pam.d/lightdm
      sudo sed -i  "/kwallet/s/^\(.*\)$/#&/g" /etc/pam.d/lightdm
      sudo sed -i  "/keyring/s/^\(.*\)$/#&/g" /etc/pam.d/lightdm-greeter
      sudo sed -i  "/kwallet/s/^\(.*\)$/#&/g" /etc/pam.d/lightdm-greeter
      ;;
    esac
  fi
}

# 2.3 ll不生效
no_bashrc(){
  change_root
  home_path
  sudo cp /etc/skel/.bashrc ./
  sudo chmod 644 .bashrc
  sudo touch .bash_profile
  echo -e "if [ -f ~/.bashrc ]; then\n\t. ~/.bashrc\nfi" | sudo tee .bash_profile
}

# 2.5 挂载开发盘
mount_disk(){
  change_root
  sudo mkdir -p /disk01
  sudo chmod -R 777 /disk01
  sudo mount /dev/nvme0n1p2 /disk01
  echo -e "打开文件系统下disk01文件夹查看"
  get_pause
}

# 2.8 更新系统
update_sys(){
  head_print=(
    ".....................系统升级-备份重要数据--没啥事儿不要乱升级...................."
    "....................................没测试过，自测.............................."
    "..........................用cat /etc/.kyinfo查看系统版本........................"
    "...................................一步一步升级................................."
  )
  options=(
    "V10-2020年的0710等升级到21年的2101"
    "V10-2021年的2101等升级到21年的2107"
    "V10SP1-还没写"
  )
  show_print "${head_print[@]}"
  show_menu "${options[@]}"
  menu_options=(
    ["0"]="return_menu"
    ["1"]="download_file sys_update_v10_0710 deb"
    ["2"]="download_file sys_update_v10_2101 deb"
  )
  menu_selection
}
# 2.9 系统功能如：exfat硬盘支持、触摸板、开机启动、校准时间
sys_func(){
  change_root
  head_print=(
    ".......................系统功能如：exfat硬盘支持、触摸板、开机启动、校准时间.........................."
  )
  options=(
    "老版本尝试增加exfat硬盘支持"
    "触摸板滑动方向改为windows一致"
    "增加rc.local"
    "校准时间--若有ntp服务"
  )
  show_print "${head_print[@]}"
  show_menu "${options[@]}"
  read -r -p "$(echo -e "$magenta 请选择数字 : $none ")" _opt
  echo
  if [[ -z $_opt ]]; then
    error
  else
    case $_opt in
    0)
      menu
      ;;
    1)
      #老版本尝试增加exfat硬盘支持
      sudo apt install exfat-fuse exfat-utils
      ;;
    2)
      #触摸板滑动方向改为windows一致
      sudo synclient VertScrollDelta=-30
      ;;
    3)
      #增加rc.local
      echo -e "\n[Install]\nWantedBy=multi-user.target\n" | sudo tee -a /lib/systemd/system/rc-local.service
      sudo touch /etc/rc.local && chmod 755 /etc/rc.local
      echo -e "#!/bin/bash\n" | sudo tee  /etc/rc.local
      sudo systemctl daemon-reload
      sudo systemctl start rc-local.service
      sudo systemctl enable rc-local.service
      echo -e "接下来可以自己编辑vim /etc/rc.local添加开机启动项了。"
      ;;
    4)
      #时间校准
      sudo ntpdate time.windows.com
      echo ""
      echo "时间校准完毕"
      ;;
    esac
  fi
}

#3.源与商店更新及修复
software_repair() {
  head_print=(
    ".......................修复系统源、软件商店相关报错.........................."
  )
  options=(
    "修复系统更新系统源---如商店里软件较少"
    "更新源时提示GPG错误"
    "更新软件源时，提示等待报头一直为0、软件源无法加锁"
    "安装软件提示失败"
    "软件商店报错---如打开商店报0001-0005等代码、dbus服务异常"
  )
  show_print "${head_print[@]}"
  show_menu "${options[@]}"
  menu_options=(
    ["0"]="return_menu"
    ["1"]="systemUpdate"
    ["2"]="softcenter_repair soft_GPG"
    ["3"]="softcenter_repair soft_lock"
    ["4"]="softcenter_repair soft_install_fail"
    ["5"]="softcenter_repair soft_wrong_code"
  )
  menu_selection
}

#3.1 系统源更新
systemUpdate() {
  head_print=(
    "...........原则上更新管理器会在开机几分钟内，顶多一小时自行向公司的源发请求更新源地址。............."
    "....................因最近系统升级，最好只用1，下面的暂时大多数失效了，还没去看....................."
  )
  options=(
    "从公司那边更新一下源地址"
    "备份现有源"
    "还原刚备份的源"
    "修复V10"
    "修复V10-sp1"
    "修复V10-sp1-hwe"
    "修复V10-sp1-麒麟9006c"
    "修复V10-sp1-990-2203"
  )
  show_print "${head_print[@]}"
  show_menu "${options[@]}"
  read -r -p "$(echo -e "$cyan 请选择0-7: $none ")" _opt
  echo
  if [[ -z $_opt ]]; then
    error
  else
    case $_opt in
    0)
      menu
      ;;
    1)
      kylin-source-update
      get_pause
      ;;
    2)
      change_root
      cp /etc/apt/sources.list /etc/apt/sources.list.bak
      ;;
    3)
      change_root
      cp /etc/apt/sources.list.bak /etc/apt/sources.list
      ;;
    4)
      change_root
      echo -e "deb http://archive.kylinos.cn/kylin/KYLIN-ALL 10.0 main restricted universe multiverse\ndeb http://archive.kylinos.cn/kylin/partner juniper main\n" | sudo tee /etc/apt/sources.list
      ;;
    5)
      echo -e "deb http://archive.kylinos.cn/kylin/KYLIN-ALL 10.1 main universe multiverse restricted\ndeb http://archive.kylinos.cn/kylin/partner 10.1 main\n" | sudo tee /etc/apt/sources.list
      ;;
    6)
      echo -e "deb http://archive.kylinos.cn/kylin/KYLIN-ALL 10.1 main restricted universe multiverse\ndeb http://archive.kylinos.cn/kylin/KYLIN-ALL 10.1-hwe main\ndeb http://archive.kylinos.cn/kylin/partner 10.1 main\n" | sudo tee /etc/apt/sources.list
      ;;
    7)
      echo -e "deb https://archive.kylinos.cn/kylin/KYLIN-ALL 10.1-kv main restricted universe multiverse\ndeb https://archive2.kylinos.cn/deb/kylin/production/PART-10_1-kirin9a0/custom/partner/10_1-kirin9a0 default all\n" | sudo tee /etc/apt/sources.list
      ;;
    8)
    echo -e "deb https://archive.kylinos.cn/kylin/KYLIN-ALL/ 10.1-wayland-2203-updates main universe restricted multiverse \n
deb https://archive.kylinos.cn/kylin/KYLIN-ALL/ 10.1-kirin990-feature main \n
deb https://archive2.kylinos.cn/deb/kylin/production/PART-10_1-kirin9a0/custom/partner/10_1-kirin9a0 default all \n" | sudo tee /etc/apt/sources.list
      ;;
    esac
  fi
  sudo apt update
}

#3.2 源、商店错误处理
softcenter_repair() {
  change_root
  local software_repair_flag=$1
  if [ "$software_repair_flag" == 'soft_GPG' ]; then
    #源GPG错误
    sudo mv /var/lib/apt/lists /var/lib/apt/list_"$timestamp"
    sudo mkdir -p /var/lib/apt/lists/partial
    sudo apt clean
    sudo apt update
    get_pause
  elif [ "$software_repair_flag" == 'soft_lock' ]; then
    #源报头为0
    sudo rm -rf /var/lib/apt/lists/*
    sudo apt update
    get_pause
  elif [ "$software_repair_flag" == 'soft_install_fail' ]; then
    #软件安装失败
    sudo apt install -f
    sudo apt-get update --fix-missing
    sudo apt autoremove
    get_pause
  elif [ "$software_repair_flag" == 'soft_wrong_code' ]; then
    #软件商店报错
    sudo apt update
    sudo apt install -y kylin-software-center --reinstall
    sudo rm /var/lib/dpkg/updates/*
    sudo rm -rf /var/lib/apt/lists/*
    sudo rm /var/lib/dpkg/lock
    sudo rm /var/lib/dpkg/lock-frontend
    sudo rm /var/cache/apt/archives/lock
    sudo dpkg --configure -a
    sudo apt install -f
    sudo apt update
    get_pause
  fi
}

#4.软件相关
software(){
  head_print=(
    ".......................我的电脑右键属性--FT2000属于ARM、3A4000属于mips.........................."
  )
  options=(
    "微信相关"
    "远程"
    "字体安装"
    "金山WPS-清理金山缓存"
    "下载腾讯会议"
    "下载新版linuxqq"
  )
  show_print "${head_print[@]}"
  show_menu "${options[@]}"
  menu_options=(
    ["0"]="return_menu"
    ["1"]="fuc_wechat"
    ["2"]="fuc_remote"
    ["3"]="fuc_fonts"
    ["4"]="fuc_kingsoft"
    ["5"]="download_file tencent_metting deb"
    ["6"]="download_file tencent_linuxqq deb"
    ["99"]="fuc_crossover"
  )
  menu_selection
}


#4.1 微信相关问题
fuc_wechat() {
  change_root
  head_print=(
    ".................微信相关问题处理................."
  )
  options=(
    "在公司源中安装微信--首选"
    "Mips--微信禁用gpu--适用于微信闪退及卡顿。"
    "Mips--微信登录提示登录失败error--在公司源中修复微信相关依赖"
    "Mips--离线修复微信相关依赖---公司源不可用情况下使用"
    "无法扫码--离线安装微信2.1.4-1版本。"
    "杀微信进程--双击微信图标无反应可尝试"
  )
  show_print "${head_print[@]}"
  show_menu "${options[@]}"
  read -r -p "$(echo -e "$magenta 请选择数字: $none")" _opt
  echo
  if [[ -z $_opt ]]; then
    error
  else
    case $_opt in
    0)
      menu
      ;;
    1)
      sudo apt update
      if [ "$sys_str" == "mips64" ]; then
        sudo apt install -y kylin-activation libkylin-activation wechat
      else
        sudo apt install -y weixin
      fi
      ;;
    2)
      cp /usr/share/applications/wechat.desktop /usr/share/applications/wechat.desktop_"$timestamp"
      sed -i 's/Exec=wechat %U/Exec=wechat  --disable-gpu  %U/' /usr/share/applications/wechat.desktop
      sed -i 's/Exec=wechat %U/Exec=wechat  --disable-gpu  %U/' /home/"$this_user"/桌面/wechat.desktop
      echo -e "请关闭微信并重新打开微信。$none \n"
      get_pause
      ;;
    3)
      sudo apt update
      sudo apt install -y kylin-activation libkylin-activation
      get_pause
      ;;
    4)
      download_file 'wx_mips_depend'
      get_pause
      ;;
    5)
      download_file 'wx_2.1.4'
      get_pause
      ;;
    6)
      sudo killall -9 weixin || sudo killall -9 wechat
      get_pause
      ;;
    esac
  fi
}

#4.2 远程相关下载
fuc_remote() {
  xhost +
  head_print=(
    ".................注意有时这些软件第一次打开有时连不上，需要右下角退出重新打开一遍................."
    ".................在左下角开始菜单--所有程序里打开向日葵或todesk................."
  )
  options=(
    "公司源中安装向日葵--首选"
    "公司源中安装todesk--首选"
    "安装Todesk的x86和arm版---此软件无mips"
    "老版本离线安装向日葵10.0版---V10MIPS常用，arm有问题也可用这个，最新版本尽量不用"
  )
  show_print "${head_print[@]}"
  show_menu "${options[@]}"
  menu_options=(
    ["0"]="return_menu"
    ["1"]="sudo apt install sunloginclient"
    ["2"]="sudo apt install todesk"
    ["3"]="download_file todesk deb"
    ["4"]="download_file sunlogin deb"
  )
  menu_selection
  change_root
  sudo apt install -f
  head_print=(
    ".................在左下角开始菜单--所有程序里打开向日葵或todesk................."
  )
  get_pause
}


#4.3 字体安装
fuc_fonts() {
  change_root
  head_print=(
    ".................安装字体，可百度搜ttf文件或在Windows中拷贝................."
  )
  options=(
    "批量安装，需将ttf文件拷到新建的文件夹下再继续执行"
    "在公司源中安装字体管理器--推荐"
    "老版本--离线安装公司开发的老版本字体管理器，使用方法:双击ttf文件右上角有安装，适用于单个ttf安装及客户自行安装字体。"
  )
  show_print "${head_print[@]}"
  show_menu "${options[@]}"
  menu_options=(
    ["0"]="return_menu"
    ["1"]="fontsowninstall"
    ["2"]="sudo apt install -y gnome-font-viewer"
    ["3"]="download_file dfonts deb"
  )
  menu_selection
  get_pause
}

#4.3.1 字体文件批量安装
fontsowninstall() {
  change_root
  desktop_path
  pwd
  sudo mkdir newfonts
  sudo mkdir -p /usr/share/fonts/newfonts
  sudo chmod -R 777 ./newfonts/
  echo
  echo -e "$magenta 请将ttf字体文件拷到newfonts文件夹中再按任意键继续进行操作:"
  get_pause
  echo
  sudo chmod -R 777 ./newfonts/
  sudo cp ./newfonts/* /usr/share/fonts/newfonts/
  sudo chmod -R 777 /usr/share/fonts/newfonts/
  sudo fc-cache /usr/share/fonts/newfonts/
  ls /usr/share/fonts/newfonts/
  echo -e "$red 应该可以用了，重新打开wps，fc相关组件未安装的需要重启下电脑。$none \n"
}
# 4.4 金山删缓存
fuc_kingsoft(){
  change_root
  quickstartoffice stop
  home_path
  sudo rm -rf .config/Kingsoft
  sudo rm -rf .local/share/Kingsoft
  quickstartoffice start
  get_pause
}
# 4.9 crossover试用
fuc_crossover(){
  change_root
  home_path
  cd ".cxoffice/" || exit
  change_root
  sudo find . -name ".eval" -exec rm -f {} \;
  sudo rm -f {}
  get_pause
}

# 5.文件查找
find_files() {
  head_print=(
    ".......................文件查找.........................."
    "..........查找的文件名或路径名保存在本目录下的find开头的txt文件中......."
  )
  options=(
    "按名称查找文件(区分大小写)"
    "按名称查找文件(不区分大小写)"
    "按文件内容查找(只在home和data目录下查找--原则上用户数据都在这俩目录)"
    "按文件内容查找(自定义目录)"
    "查找源里的软件包"
    "查找本机所有安装的软件包"
    "查找本机是否安装软件包--如输入linuxqq看有没有安装qq"
  )
  show_print "${head_print[@]}"
  show_menu "${options[@]}"
  menu_options=(
    ["0"]="return_menu"
    ["1"]="findname file_case"
    ["2"]="findname file_nocase"
    ["3"]="findname file_content"
    ["4"]="findname file_own_content"
    ["5"]="findname soft_source"
    ["6"]="findname soft_local_all"
    ["7"]="findname soft_local"
  )
  menu_selection
}

findname() {
  change_root
  local file_flag=$1
  if [ "$file_flag" == 'file_case' ]; then
    read -r -p "$(echo -e "请输入要查找的文件名:")" name
    sudo find /home/ -name "$name*" -print | tee find"$name".txt
    return_menu
  elif [ "$file_flag" == 'file_nocase' ]; then
    read -r -p "$(echo -e "请输入要查找的文件名:")" name
    sudo find /home/ -iname "$name*" -print | tee find"$name".txt
    return_menu
  elif [ "$file_flag" == 'file_content' ]; then
    read -r -p "$(echo -e "请输入要查找的内容:")" content
    sudo grep -rl "$content" /home /data | tee find"$content".txt
    return_menu
  elif [ "$file_flag" == 'file_own_content' ]; then
    read -r -p "$(echo -e "请输入要查找的内容:")" content
    read -r -p "$(echo -e "请输入要查找的位置，多个位置用空格隔开:")" loc
    sudo grep -rl "$content" "$loc" | tee find"$content".txt
    return_menu
  elif [ "$file_flag" == 'soft_source' ]; then
    read -r -p "$(echo -e "请输入要查找的内容:")" soft_source
    sudo apt search "$soft_source"
    return_menu
  elif [ "$file_flag" == 'soft_local_all' ]; then
    echo -e "安装的软件包输出到文件中"
    sudo dpkg --get-selections | tee installsoftware.txt
    return_menu
  elif [ "$file_flag" == 'soft_local' ]; then
    read -r -p "$(echo -e "请输入要查找的内容:")" soft_local
    sudo dpkg --get-selections | grep "$soft_local"
    return_menu
  fi
}

#6.打印机驱动安装

install_cups(){
  change_root
  head_print=(
    ".......................此处安装什么驱动需咨询麒麟员工.........................."
    "...........驱动去麒麟官网查https://eco.kylinos.cn/zoology/commercial.html?system_class=1..........."
  )
  options=(
    "安装惠普3.16.3"
    "安装惠普3.22.x或最新版"
  )
  show_print "${head_print[@]}"
  show_menu "${options[@]}"
  menu_options=(
    ["0"]="return_menu"
    ["1"]="sudo apt install hp-printer-hplip-3.16.3"
    ["2"]="sudo apt install hplip"
  )
  menu_selection
}

#7.上传到ftp
up_to_ftp(){
  head_print=(
    "..............本脚本作用：查找数据盘和/home/文件夹下某时间段修改过的文件并打包................."
    "..............注意：比如2021年创建的，但是2022年修改过的不算.................................."
    "..............注意：数据盘应保有足够大的空间执行打包操作，空间不够大的不适合这个脚本。..........."
    "..............备份的数据多则备份时间长，备份中不要按回车，请耐心等待............................"
  )
  options=(
    "第一次执行请直接按1打包不成功重试"
    "打包成功，但是因为种种原因没能上传成功请按2"
  )
  show_print "${head_print[@]}"
  show_menu "${options[@]}"
	read -r -p "$(echo -e "${red} 确定上面的都读过了，请选择数字: ${none}")" _opt
	echo
  if [[ -z $_opt ]]; then
    error
  else
    case $_opt in
      1)
        pack_file
        rename_file
        remote_file
        ;;
      2)
        get_file_name
        remote_file
        ;;
    esac
  fi
}

#7.1 打包文件
pack_file(){
  read -r -p "$(echo -e "请输入要打包文件的起始时间如 2022-01-01 :")" starttime
  read -r -p "$(echo -e "请输入要打包文件的结束时间如 2023-01-01 :")" endtime
	find  /data/ /home/ ! -name '.*' -a ! -regex '.*/\.[^/]*/.*' -type f -print0 -newermt  "$starttime" ! -newermt "$endtime" | xargs -0 tar -czPf /data/test.tar.gz
  echo
	echo -e "已经打包好了，在/data/下，目前叫test.tar.gz"
}
#7.2 重命名文件
rename_file(){
  head_print=(
    ".........接下来我们将给你即将打包的文件取个名字，比如：张三................."
    ".........建议取自己的名字，但不要跟其他人的名称重复,尤其不要取111,222之类的名......."
    ".........切换好你的输入法..........."
    ""
  )
  show_print "${head_print[@]}"
	read -r -p "$(echo -e "请输入你的文件名:${none}")" name
	echo
	echo -e "这个文件将被改为：${red}${name}.tar.gz ${none}，按任意键继续进行操作:"
  sleep 10
	echo
  get_pause
	echo
	mv /data/test.tar.gz /data/"$name".tar.gz
	chmod 755 /data/"$name".tar.gz
  echo
	echo -e "已经重命名好了，此文件名为：${red}${name}.tar.gz${none}"
}

#7.3 推到远程ftp服务器
remote_file(){
  head_print=(
    ".........现在即将把这个文件传到ftp服务器上去................."
    ".........比如ftp目录为文件夹:upload/2021......."
    ".........为了安全此脚本内不内置服务器密码，需要自己输入一下..........."
    ".........上传的文件大则上传时间长，请耐心等待........."
    ".........了解后按任意键继续进行操作:........."
  )
  show_print "${head_print[@]}"
  get_pause
	echo
  read -r -p "$(echo -e "请输入服务器IP地址如 192.168.210.5 ")" addr
	echo
  read -r -p "$(echo -e "请输入服务器端地址如 /ftpdata/upload/2021/ ")" addr_home
  echo
	echo -e "请输入服务器密码:"
	echo
  #scp /data/$name.tar.gz  root@192.168.210.5:/ftpdata/upload/2021/
	scp /data/"$name".tar.gz  root"$addr":"$addr_home"
	echo
	echo -e " 已经上传好了，请打开左下角开始菜单，找到FileZilla这个FTP软件查看"
	echo
	rm -rif /data/"$name".tar.gz
  echo
}

#7.4 获取文件名
get_file_name(){
	echo
	echo -e "${cyan} 比如说你的数据盘下的文件为：${red}张三.tar.gz${none}${cyan}，则直接输入：${red}张三${none}"
	echo
	read -r -p "$(echo -e " 请输入文件名：")" name
	echo
	echo -e " 要上传的文件名为：${red}${name}.tar.gz ${none}，确定则按任意键继续进行操作:"
	echo
  get_pause
	echo
}

#8.服务器相关
about_server(){
  change_root
  head_print=(
    ".......................本脚本作用：服务器相关.........................."
  )
  options=(
    "修复yum makecache报错，如error: rpmdb: xxx failed"
  )
  show_print "${head_print[@]}"
  show_menu "${options[@]}"
	read -r -p "$(echo -e "${red} 请选择数字: ${none}")" _opt
	echo
  if [[ -z $_opt ]]; then
    error
  else
    case $_opt in
      1)
        sudo rm -f /var/lib/rpm/__db*
        sudo db_verify /var/lib/rpm/Packages
        sudo rpm --rebuilddb
        ;;
    esac
  fi
}

get_download_link() {
  local download_flag="$1"

  case $download_flag in
    "wx_mips_depend")
      if [ "$sys_str" == "mips64" ]; then
        echo "https://kylin-1253703239.cos.ap-nanjing.myqcloud.com/Tencent/Wechat/Mips/wechat-mips2.0-depend.zip"
      fi
      ;;
    "act_v10_0710")
      if [ "$sys_str" == "mips64" ]; then
        echo "https://kylin-1253703239.cos.ap-nanjing.myqcloud.com/Activation/V10/old/act_V10_mips_0710.zip"
      elif [ "$sys_str" == "aarch64" ]; then
        echo "https://kylin-1253703239.cos.ap-nanjing.myqcloud.com/Activation/V10/old/act_V10_arm_0710.zip"
      fi
      ;;
    "act_v10_0710_2")
      if [ "$sys_str" == "aarch64" ]; then
        echo "https://kylin-1253703239.cos.ap-nanjing.myqcloud.com/Activation/V10/old/act_V10_arm_0710_2.zip"
      fi
      ;;
    "act_v10_1026")
      if [ "$sys_str" == "mips64" ]; then
        echo "https://kylin-1253703239.cos.ap-nanjing.myqcloud.com/Activation/V10/old/act_V10_mips_1026.zip"
      elif [ "$sys_str" == "aarch64" ]; then
        echo "https://kylin-1253703239.cos.ap-nanjing.myqcloud.com/Activation/V10/old/act_V10_arm_1026.zip"
      fi
      ;;
    "act_v10_1026_2")
      if [ "$sys_str" == "mips64" ]; then
        echo "https://kylin-1253703239.cos.ap-nanjing.myqcloud.com/Activation/V10/old/act_V10_mips_1026_2.zip"
      fi
      ;;
    "act_v10_2107")
      if [ "$sys_str" == "mips64" ]; then
        echo "https://kylin-1253703239.cos.ap-nanjing.myqcloud.com/Activation/V10/2107/act_v10_mips_2107.zip"
      elif [ "$sys_str" == "aarch64" ]; then
        echo "https://kylin-1253703239.cos.ap-nanjing.myqcloud.com/Activation/V10/2107/act_v10_arm_2107.zip"
      elif [ "$sys_str" == "x86_64" ]; then
        echo "https://kylin-1253703239.cos.ap-nanjing.myqcloud.com/Activation/V10/2107/act_v10_x86_2107.zip"
      fi
      ;;
    "act_v10_2203")
      if [ "$sys_str" == "mips64" ]; then
        echo "https://kylin-1253703239.cos.ap-nanjing.myqcloud.com/Activation/V10/2203/act_V10_mips_2203.zip"
      elif [ "$sys_str" == "aarch64" ]; then
        echo "https://kylin-1253703239.cos.ap-nanjing.myqcloud.com/Activation/V10/2203/act_V10_arm_2203.zip"
      elif [ "$sys_str" == "x86_64" ]; then
        echo "https://kylin-1253703239.cos.ap-nanjing.myqcloud.com/Activation/V10/2203/act_v10_x86_2203.zip"
      fi
      ;;
    "act_v10sp1_2107")
      if [ "$sys_str" == "mips64" ]; then
        echo "https://kylin-1253703239.cos.ap-nanjing.myqcloud.com/Activation/V10SP1/2107/act_V10SP1_mips_2107.zip"
      elif [ "$sys_str" == "aarch64" ]; then
        echo "https://kylin-1253703239.cos.ap-nanjing.myqcloud.com/Activation/V10SP1/2107/act_V10SP1_arm_2107.zip"
      elif [ "$sys_str" == "x86_64" ]; then
        echo "https://kylin-1253703239.cos.ap-nanjing.myqcloud.com/Activation/V10SP1/2107/act_V10SP1_x86_2107.zip"
      fi
      ;;
    "act_v10sp1_2203")
      if [ "$sys_str" == "mips64" ]; then
        echo "https://kylin-1253703239.cos.ap-nanjing.myqcloud.com/Activation/V10SP1/2203/act_V10SP1_mips_2203.zip"
      elif [ "$sys_str" == "aarch64" ]; then
        echo "https://kylin-1253703239.cos.ap-nanjing.myqcloud.com/Activation/V10SP1/2203/act_V10SP1_arm_2203.zip"
      elif [ "$sys_str" == "x86_64" ]; then
        echo "https://kylin-1253703239.cos.ap-nanjing.myqcloud.com/Activation/V10SP1/2203/act_V10SP1_x86_2203.zip"
      elif [ "$sys_str" == "loongarch64" ]; then
        echo "https://kylin-1253703239.cos.ap-nanjing.myqcloud.com/Activation/V10SP1/2203/act_V10SP1_loongarch_2203.zip"
      fi
      ;;
    "act_v10sp1_2303")
      if [ "$sys_str" == "mips64" ]; then
        echo "https://kylin-1253703239.cos.ap-nanjing.myqcloud.com/Activation/V10SP1/2303/act_V10SP1_mips_2303.zip"
      elif [ "$sys_str" == "aarch64" ]; then
        echo "https://kylin-1253703239.cos.ap-nanjing.myqcloud.com/Activation/V10SP1/2303/act_V10SP1_arm_2303.zip"
      elif [ "$sys_str" == "x86_64" ]; then
        echo "https://kylin-1253703239.cos.ap-nanjing.myqcloud.com/Activation/V10SP1/2303/act_V10SP1_x86_2303.zip"
      elif [ "$sys_str" == "loongarch64" ]; then
        echo "https://kylin-1253703239.cos.ap-nanjing.myqcloud.com/Activation/V10SP1/2303/act_V10SP1_loongarch_2303.zip"
      fi
      ;;
    "kelu_xuke")
      case $sys_str in
        "mips64")
          echo "https://kylin-1253703239.cos.ap-nanjing.myqcloud.com/kelu/xuke_mips.zip"
          ;;
        "aarch64")
          echo "https://kylin-1253703239.cos.ap-nanjing.myqcloud.com/kelu/xuke_arm.zip"
          ;;
        "x86_64")
          echo "https://kylin-1253703239.cos.ap-nanjing.myqcloud.com/kelu/xuke_x86.zip"
          ;;
        "loongarch64")
          echo "https://kylin-1253703239.cos.ap-nanjing.myqcloud.com/kelu/xuke_loongarch64.zip"
          ;;
      esac
      ;;
    "dfonts")
      if [ "$sys_str" == "mips64" ]; then
        echo "https://kylin-1253703239.cos.ap-nanjing.myqcloud.com/fonts/font_3.16.2-3_mips64el.deb"
      elif [ "$sys_str" == "aarch64" ]; then
        echo "https://kylin-1253703239.cos.ap-nanjing.myqcloud.com/fonts/font_3.16.2-3_arm64.deb"
      elif [ "$sys_str" == "x86_64" ]; then
        echo "https://kylin-1253703239.cos.ap-nanjing.myqcloud.com/fonts/font_3.16.2-3_amd64.deb"
      else
        echo "暂不支持" && exit 1
      fi
      ;;
    "todesk")
      if [ "$sys_str" == "aarch64" ]; then
        echo "https://newdl.todesk.com/linux/todesk-v4.3.1.0-arm64.deb"
      elif [ "$sys_str" == "x86_64" ]; then
        echo "https://newdl.todesk.com/linux/todesk-v4.3.1.0-amd64.deb"
      else
        echo "暂不支持" && exit 1
      fi
      ;;
    "sunlogin")
      if [ "$sys_str" == "mips64" ]; then
        echo "https://kylin-1253703239.cos.ap-nanjing.myqcloud.com/sunlogin/10.x/sunloginclient-10.0.2.24779_kylin_mips.deb"
      elif [ "$sys_str" == "aarch64" ]; then
        if [ "$cpu_name" == "Kirin990" ]; then
          if [ "$os_version_small" == "40222" ]; then
            echo "https://kylin-1253703239.cos.ap-nanjing.myqcloud.com/sunlogin/11.x/sunlogin-990-2203.zip"
            #TUDO
          else
            echo "https://kylin-1253703239.cos.ap-nanjing.myqcloud.com/sunlogin/11.x/sunloginclient_11.0.0.36662_arm64_kirin990_1228.deb"
          fi
        elif [ "$cpu_name" == "HUAWEI Kirin 9006C" ]; then
          echo "https://kylin-1253703239.cos.ap-nanjing.myqcloud.com/sunlogin/11.x/sunloginclient_11.0.0.36662_arm64_kirin9a0_20220221.deb"
      else
        echo "https://kylin-1253703239.cos.ap-nanjing.myqcloud.com/sunlogin/10.x/sunloginclient-10.0.2.24779_kylin_arm64.deb"
      fi
      elif [ "$sys_str" == "x86_64" ]; then
        echo "https://kylin-1253703239.cos.ap-nanjing.myqcloud.com/sunlogin/10.x/sunloginclient-10.0.2.24779_kylin_x86_64.deb"
      else
        echo "暂不支持" && exit 1
      fi
      ;;
    "wx_2.1.4")
      if [ "$sys_str" == "x86_64" ]; then
        echo "http://archive2.kylinos.cn/deb/kylin/production/PART-V10-SP1/custom/partner/V10-SP1/pool/all/weixin_2.1.4-1_amd64.deb"
      elif [ "$sys_str" == "aarch64" ]; then
        echo "http://archive2.kylinos.cn/deb/kylin/production/PART-V10-SP1/custom/partner/V10-SP1/pool/all/weixin_2.1.4-1_arm64.deb"
      else
        echo "暂不支持" && exit 1
      fi
      ;;
    "tencent_metting")
      if [ "$sys_str" == "x86_64" ]; then
        echo "https://updatecdn.meeting.qq.com/cos/f5610901e43a837d6dfab5701024dd13/TencentMeeting_0300000000_3.15.0.401_x86_64_default.publish.deb"
      elif [ "$sys_str" == "aarch64" ]; then
        echo "https://updatecdn.meeting.qq.com/cos/369d6e28f19bfeaf11f13a138236f3f5/TencentMeeting_0300000000_3.15.0.401_arm64_default.publish.deb"
      elif [ "$sys_str" == "loongarch64" ]; then
        echo "https://updatecdn.meeting.qq.com/cos/a44242512aac28a3bc73749be45711b7/TencentMeeting_0300000000_3.15.0.401_loongarch64_default.publish.deb"
      else
        echo "暂不支持" && exit 1
      fi
      ;;
    "tencent_linuxqq")
      if [ "$sys_str" == "x86_64" ]; then
        echo "https://dldir1.qq.com/qqfile/qq/QQNT/80d33f88/linuxqq_3.1.2-12912_amd64.deb"
      elif [ "$sys_str" == "aarch64" ]; then
        echo "https://dldir1.qq.com/qqfile/qq/QQNT/80d33f88/linuxqq_3.1.2-12912_arm64.deb"
      elif [ "$sys_str" == "loongarch64" ]; then
        echo "https://dldir1.qq.com/qqfile/qq/QQNT/c6032ac7/linuxqq_3.1.0-9332_loong64.deb"
      else
        echo "暂不支持" && exit 1
      fi
      ;;
    "sys_update_v10_0710")
      if [ "$sys_str" == "x86_64" ]; then
        echo "http://archive.kylinos.cn/kylin-tmp-upgrade/v10-200710-210122/pool/kylin-update-patch_10-ori-20210209-200710-210122_amd64.deb"
      elif [ "$sys_str" == "aarch64" ]; then
        echo "http://archive.kylinos.cn/kylin-tmp-upgrade/v10-200710-210122/pool/kylin-update-patch_10-ori-20210209-200710-210122_arm64.deb"
      elif [ "$sys_str" == "mips64" ]; then
        echo "http://archive.kylinos.cn/kylin-tmp-upgrade/v10-200710-210122/pool/kylin-update-patch_10-ori-20210209-200710-210122_mips64el.deb"
      else
        echo "暂不支持" && exit 1
      fi
      ;;
    "sys_update_v10_2101")
      if [ "$sys_str" == "x86_64" ]; then
        echo "http://archive.kylinos.cn/kylin-tmp-upgrade/V10_2101-2107/pool/kylin-update-desktop_10-ori-2101-2107_amd64.deb"
      elif [ "$sys_str" == "aarch64" ]; then
        echo "http://archive.kylinos.cn/kylin-tmp-upgrade/V10_2101-2107/pool/kylin-update-desktop_10-ori-2101-2107_arm64.deb"
      elif [ "$sys_str" == "mips64" ]; then
        echo "http://archive.kylinos.cn/kylin-tmp-upgrade/V10_2101-2107/pool/kylin-update-desktop_10-ori-2101-2107_mips64el.deb"
      else
        echo "暂不支持" && exit 1
      fi
      ;;
    *)
      echo "暂不支持" && exit 1
      ;;
  esac
}

download_file() {
  change_root
  local download_flag=$1
  local down_format_flag=$2
  downzip_tmp="/tmp/$download_flag/"
  if [[ $down_format_flag == "act_zip" || $down_format_flag == "deb_zip" ]]; then
    download_tmp_file="/tmp/$download_flag/$download_flag.zip"
  elif [[ $down_format_flag == "deb" ]]; then
    download_tmp_file="/tmp/$download_flag/$download_flag.deb"
  fi
  mkdir -p "$downzip_tmp"

  download_link=$(get_download_link "$download_flag")

  echo "$download_flag"
  echo "$down_format_flag"
  echo "$download_tmp_file"
  echo "$download_link"

  if ! wget --no-check-certificate -O "$download_tmp_file" "$download_link"; then
    echo -e "$red 下载文件失败..$none" && exit 1
  fi
  cd "$downzip_tmp" || exit
  
  if [[ $down_format_flag == "act_zip" || $down_format_flag == "deb_zip" ]]; then
    unzip -o "$download_flag".zip
    if [[ $down_format_flag == "deb_zip" ]]; then
      sudo chmod 777 -- ./*.deb
      sudo dpkg -i -- ./*.deb
      sudo dpkg -i -- ./*.deb
    elif [[ $down_format_flag == "act_zip" ]]; then
      sudo cp "$downzip_tmp"/.kyinfo "$downzip_tmp"/LICENSE /etc/
      echo -e "$red 应该是修改完了，关闭之前的激活页面，重新打开激活页面激活一下吧。$none \n"
    fi
  elif [[ $down_format_flag == "deb" ]]; then
    sudo chmod 777 -- ./*.deb
    sudo dpkg -i -- ./*.deb
  fi

  if [ "$download_flag" == "dfonts" ]; then
    echo -e "$red 应该是下载并安装成功了，右键ttf文件在字体查看器中打开，右上角有安装按钮。$none \n"
  fi
  get_pause
}

#程序入口
menu() {
  while :; do
    head_print=(
      ".......................系统环境异常修复.........................."
      "..........我的电脑右键属性--FT2000属于ARM、3A4000属于mips........"
    )
    options=(
      "查看系统信息--系统基本信息、实时网速等"
      "修复系统问题--激活问题、桌面无状态栏、图标等"
      "修复软件源、软件少、软件无法安装、软件商店报错等问题"
      "软件相关--微信、远程、字体、金山等"
      "查找文件或安装包"
      "打印机相关"
      "上传文件到FTP"
      "服务器相关"
    )
    show_print "${head_print[@]}"
    show_menu "${options[@]}"
    menu_options=(
      ["0"]="return_menu"
      ["1"]="system_message"
      ["2"]="system_repair"
      ["3"]="software_repair"
      ["4"]="software"
      ["5"]="find_files"
      ["6"]="install_cups"
      ["7"]="up_to_ftp"
      ["8"]="about_server"
    )
    menu_selection
  done
}
args=$1
[ -z "$1" ] && args="menu"
case $args in
menu)
  menu
  ;;
esac
